package org.xiaohanghu.starter;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

/**
 * @author xiaohanghu
 */
public class PropertiesUtils {

	public static Properties loadProperties(String filePath)
			throws FileNotFoundException, IOException {
		File propertiesFile = new File(filePath);
		return loadProperties(propertiesFile);
	}

	public static Properties loadProperties(File file)
			throws FileNotFoundException, IOException {
		InputStream inputStream = new BufferedInputStream(new FileInputStream(
				file));
		return loadProperties(inputStream);
	}

	public static Properties loadProperties(URL url) throws IOException {
		URLConnection urlConnection = url.openConnection();
		InputStream inputStream = urlConnection.getInputStream();
		return loadProperties(inputStream);
	}

	public static Properties loadProperties(InputStream inputStream)
			throws IOException {
		Properties properties = new Properties();
		properties.load(inputStream);
		return properties;
	}

	/**
	 * 将properties全部put到map中
	 * */
	public static void addAllProperties(Map<String, String> map,
			Properties properties) {
		for (Entry<Object, Object> entry : properties.entrySet()) {
			Object key = entry.getKey();
			Object value = entry.getValue();
			if (null != key && null != value) {
				map.put(key.toString(), value.toString());
			}
		}
	}

}
